/***************************************************************************
 *   Copyright (C) 2009 by Dead_Body                                       *
 *   jamesholodnak@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "zlib.h"		//for crc32
#include "defines.h"
#include "pce/rom.h"

#include "system/file.h"
#include "pce/pce.h"

//thanks tnse for this, long ago
u32 rom_createmask(u32 size)
{
	u32 x,y,result;

	result = size - 1;
	for(x=0;x<32;x++) {
		y = 1 << x;
		if(y == size)
			return(result);
		if(y > size) {
			result = y - 1;
			return(result);
		}
	}
	return(0xFFFFFF);
}

rom_t *rom_load(const char *filename)
{
	int fd;						//file handle
	rom_t *ret = 0;			//rom data struct
	u8 header[16];
	char *p;

	//parse out the rom filename
	if((p = strrchr(filename,'/')) == 0) {
		if((p = strrchr(filename,'\\')) == 0) {
			p = (char*)filename;
		}
		else
			p++;
	}
	else
		p++;

	//allocate memory for rom struct and zero it out
	ret = (rom_t*)malloc(sizeof(rom_t));
	memset(ret,0,sizeof(rom_t));

	//copy the rom filename and display an informative message
	strcpy(ret->filename,p);
	log_message("loading '%s'...\n",ret->filename);

	//try to open the file
	if((fd = file_open((char*)filename,"rb")) == -1) {
		log_message("rom_load:  cannot open rom image\n");
		rom_unload(ret);
		return(0);
	}

	//seek to end of file and get size, then calculate and seek to beginning of rom
	file_seek(fd,0,SEEK_END);
	ret->size = file_tell(fd);
	log_message("file size is $%X (%d)\n",ret->size,ret->size);
	file_seek(fd,ret->size & 0x1FFF,SEEK_SET);
	ret->size &= ~0x1FFF;

	//allocate and load rom data
	ret->data = (u8*)malloc(ret->size);
	file_read(fd,ret->data,ret->size);

	log_message("pce image '%s' loaded ok (%d 8k banks)\n",ret->filename,ret->size / 8192);
	file_close(fd);
	return(ret);
}

void rom_unload(rom_t *r)
{
	if(r) {
		if(r->data)
			free(r->data);
		memset(r,0,sizeof(rom_t));
		free(r);
	}
}

u32 rom_crc32(rom_t *r)
{
	return(crc32(crc32(0L,0,0),r->data,r->size));
}

int rom_checkdb(rom_t *ret,int whichdb)
{
	int i;
	u32 crc;

	//check rom db
	if(whichdb == 0) {
/*		crc = rom_crc32(ret);
		for(i=0;rom_crcs[i].name;i++) {
			rom_crc_t *rc = &rom_crcs[i];

			if(rc->crc == crc) {
				log_message("rom found in database:\n");
				log_message("  name: %s\n",rc->name);
				strcpy(ret->name,rc->name);
				ret->indatabase = 1;
				return(1);
			}
		}*/
	}

	//check cd db
	else if(whichdb == 1) {
	}

	strcpy(ret->name,ret->filename);
	return(0);
}
 
